/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sip;

import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author arianareyessantiago
 */
public class BasalInsulinRunnable implements Runnable{
    
    private BasalProfile basalProfileToBeActivated;
    
    public void basalInsulinRunnable(BasalProfile basalProfileToActivate){
        basalProfileToBeActivated = basalProfileToActivate;
        
    }
   
    
    InitSip initSip = new InitSip();
    /**
     *
     */
    private static volatile boolean running = true;
    private static volatile boolean hourRunning = true;
    private static volatile boolean flag = true;
    private static double insulinAmount;
    private static double insulinAmountPerSec=0;
    private static int currentMinute;
    private static int noOfSeconds;
    private boolean suspendFlag = false;
    
    //private double totalInsulinAmountTemp;

    Alarm alarm = new Alarm();
    
    //Boolean runFlag = true
    
    
    public void run(){
        //while(!CheckStatus.bubbles){
            while(hourRunning)
            {
                while(flag)
                {
                    //double insulinAmount = 60;
                    //captures the insulin values from basalProfileArray
                    insulinAmount = basalProfileToBeActivated.mProfile[Clock.GetCurrentHour()];
                    SipLogger.Instance().info("Insulin Amount For this hour "+ basalProfileToBeActivated.getProfileName()+insulinAmount);
                    
                    // insulin amount to be given per sec
                    insulinAmountPerSec = roundFiveDecimals(insulinAmount / 3600);
                    //System.out.println("BasalInsulinPer Sec: " + insulinAmountPerSec);
                    currentMinute = Clock.GetCurrentMinute();
                    // no of seconds in (60 - currentMinute) minute
                    noOfSeconds = (60 - currentMinute)*60;
                    //System.out.println(noOfSeconds);
                    
                   while(running){


                        SipLogger.Instance().debug("The BasalRunnable has started.");

                        try{
                            for(int i = 0; i < noOfSeconds; i++){
                                if(running){
                                    if(!CheckStatus.bubbles){
                                        if(InitSip.totalInsulinAmount <= 1)
                                        {
                                            //Alarm alarm = new Alarm();
                                            //alarm.setAlarm("Insuffucient amount of Insulin in reservoir . Fill reservoir.Stopping "); 
                                            suspendFlag = true;
                                        }
                                        else
                                        {
                                        InitSip.totalInsulinAmount= roundFiveDecimals(InitSip.totalInsulinAmount - insulinAmountPerSec);
                                        //System.out.println("Total Insulin Amount after Basal: " + InitSip.totalInsulinAmount);
                                        }
                                        Thread.sleep(1000);
                                        synchronized(this) {
                                            while(suspendFlag) {
                                                wait();
                                            }
                                        }
                                        
                                    }
                                    else{
                                    running = false;
                                    flag = false;
                                    hourRunning = false;
                                    }
                                }  
                            }
                        }
                        catch (InterruptedException e) { 
                            SipLogger.Instance().debug("BasalInsulinRunnable has been interrupted");       
                        } 
                       initSip.writeCurrentValues();

                       //SipLogger.Instance().debug("I left the Loop now.");
                       //Bolus.bolusInsulin = 0;
                       running = false;
                        }  
            //CheckStatus check = new CheckStatus();

                    running = true;
                }
        //}
        }
        SipLogger.Instance().debug("BasalInsulinRunnable has stopped.");
               
    }
    public void mysuspend() {
            SipLogger.Instance().debug("BasalInsulinRunnable thread got suspended.");
                    suspendFlag = true;
                    flag = false;
                }
   public synchronized void myresume() {
       SipLogger.Instance().debug("BasalInsulinRunnable thread is Resumed.");
                    suspendFlag = false;
                    flag = true;
                    currentMinute = Clock.GetCurrentMinute();
                    // no of seconds i(60 - currentMinute) minute
                    noOfSeconds = (60 - currentMinute)*60;
                    System.out.println(noOfSeconds);
                    notify();
               }
    public void pauseThread() throws InterruptedException
    {
        SipLogger.Instance().debug("BasalInsulinRunnable was Paused.");
        //hourRunning = false;
        
        running = false;
        hourRunning = true;
        
    }

    public void resumeThread()
    {
        SipLogger.Instance().debug("BasalInsulinRunnable was Resumed.");
        SipLogger.Instance().info("Resume--Insulin Amount For this hour "+ basalProfileToBeActivated.getProfileName()+ " " + insulinAmount);
        
      insulinAmountPerSec = insulinAmount / 3600;
      currentMinute = Clock.GetCurrentMinute();
      insulinAmount = basalProfileToBeActivated.mProfile[Clock.GetCurrentHour()];
      insulinAmountPerSec = insulinAmount / 3600;
        flag = true;
        hourRunning = true;
        running = true;
        
        
    }
    public void terminate(){
        SipLogger.Instance().debug("BasalInsulinRunnable was terminated.");
        running = false;
        flag = false;
        hourRunning = false;
        
    }
    public double roundFiveDecimals(double d){
        DecimalFormat fiveDForm = new DecimalFormat("#.#####");
        return Double.valueOf(fiveDForm.format(d));
    }
    
    

}    
    
